package com.china08.yxyapi.api.temp;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.sws.SwsPvForDailyAnalyze;
import com.china08.yxyapi.entity.mg.sws.SwsPvForTotalAnalyze;
import com.china08.yxyapi.repository.mg.SwsPvForTotalAnalizeRepository;

@CrossOrigin
@RestController
public class SwsSchWebsiteTongjiJiaoyanController {

	private static final Logger log = LoggerFactory.getLogger(SwsSchWebsiteTongjiJiaoyanController.class);
	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	private SwsPvForTotalAnalizeRepository swsPvForTotalAnalizeRepository;

	/**
	 * 2017-05-05/2017-05-06
	 * 
	 * @param request
	 * @param response
	 * @param begin
	 * @param end
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/api_yw/temp/sws/website/tongji/school/jiaoyan", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object process(HttpServletRequest request, HttpServletResponse response) throws ParseException {
		log.info("====学校网站pv统计校正开始===");
		TypedAggregation<SwsPvForDailyAnalyze> aggregationZong = Aggregation.newAggregation(SwsPvForDailyAnalyze.class,
				Aggregation.group("schoolId").sum("pv").as("pv").first("schoolId").as("schoolId"));
		AggregationResults<PvForDaily> resultZong = mongoTemplate.aggregate(aggregationZong, PvForDaily.class);
		List<PvForDaily> dateModelLst = resultZong.getMappedResults();
		if (dateModelLst != null && dateModelLst.size() > 0) {
			List<PvForDaily> pvList = new ArrayList<>();
			Integer index = 0;
			for (PvForDaily pvForDaily : dateModelLst) {
				pvList.add(pvForDaily);
				index++;
				if (index == 200) {
					for (PvForDaily en : pvList) {
						SwsPvForTotalAnalyze entity = swsPvForTotalAnalizeRepository
								.findTop1BySchoolIdOrderByPvDesc(en.schoolId);
						if (entity != null) {
							entity.setPv(en.pv);
							swsPvForTotalAnalizeRepository.save(entity);
						}
					}
					pvList = new ArrayList<>();
					index = 0;
				}
			}
			if (pvList.size() > 0) {
				for (PvForDaily en : pvList) {
					SwsPvForTotalAnalyze entity = swsPvForTotalAnalizeRepository
							.findTop1BySchoolIdOrderByPvDesc(en.schoolId);
					if (entity != null) {
						entity.setPv(en.pv);
						swsPvForTotalAnalizeRepository.save(entity);
					}
				}
			}
		} else {
			log.info("");
		}
		log.info("===========学校网站pv统计校正结束========");
		return "完成";
	}

	class PvForDaily {
		public Long pv;
		public String schoolId;
	}
}
